package com.vans.practice.aqs_lock;

import org.checkerframework.checker.units.qual.C;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;

/**
 * @Description: java自己实现的锁
 * @Note:
 * @Author elle
 * @Date 2020/10/5 17:56
 */
public class CountDownLatchTest {
    private static int m;
    private static Lock lock = new MyAQSLock();
    private static CountDownLatch latch = new CountDownLatch(100);

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[100];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                try {
                    lock.lock();
                    for (int j = 0; j < 100; j++) {
                        m++;
                    }
                } finally {
                    lock.unlock();
                }
                latch.countDown();
            });
        }

        for (Thread t: threads) t.start();
        latch.await();
        System.out.println(m);
    }
}
